package controller.search;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import dto.search.ProfileDTO;
import dto.search.SearchDTO;

import persistence.ProfileDAO;

import entity.Profile;

@ManagedBean(name = "searchController")
@SessionScoped
public class SearchController implements Serializable {

	private static final long serialVersionUID = -1467477837785827258L;

	public ProfileDTO searchByID(int profileId, String tenantId)
			throws Exception {
		ProfileDAO dao = new ProfileDAO();
		Profile profile = dao.findById(profileId, tenantId);
		ProfileDTO dto = new ProfileDTO(profile.getIdProfile(),
				profile.getTenantid(), profile.getFirstname(),
				profile.getLastname(), profile.getGender(), profile.getDob(),
				profile.getAge(), profile.getBirthplace(), profile.getHeight(),
				profile.getComplexion(), profile.getEducationlevel(),
				profile.getEducation(), profile.getMaritalstatus(),
				profile.getPaternalgotram(), profile.getMaternalgotram(),
				profile.getStar(), profile.getRasi(), profile.getAboutself(),
				profile.getOccupation(), profile.getIncome()+"",
				profile.getFathername(), profile.getFatherprofession(),
				profile.getMothername(), profile.getMotherprofession(),
				profile.getEmail(), profile.getMobileno(),
				profile.getAddress(), "https://scontent-b-sin.xx.fbcdn.net/hphotos-xpa1/t31.0-8/1075318_574073522687896_26281572_o.jpg", 
				"http://www.youtube.com/v/XGSy3_Czz8k",profile.getReccrtts(),profile.getReclstupdatets());
		return dto;
	}
	
	public List<ProfileDTO> searchByQuery(SearchDTO searchDTO) throws Exception{
		ProfileDAO dao=new ProfileDAO();
		StringBuffer queryStr=new StringBuffer("from Profile where tenantid = '"+searchDTO.getTenantID()+"' ");
		if(StringUtils.isNotBlank(searchDTO.getGender()))
			queryStr.append(" And gender = '"+searchDTO.getGender()+"' ");
		if(searchDTO.getMaxHeight()!=0)
			queryStr.append(" And height <= '"+searchDTO.getMaxHeight()+"' ");
		if(searchDTO.getMinHeight()!=0)
			queryStr.append(" And height >= '"+searchDTO.getMinHeight()+"' ");
		if(searchDTO.getMaxAge()!=0)
			queryStr.append(" And age <= '"+searchDTO.getMaxAge()+"' ");
		if(searchDTO.getMinAge()!=0)
			queryStr.append(" And age >= '"+searchDTO.getMinAge()+"' ");
		if(searchDTO.getIncome()!=0)
			queryStr.append(" And income >= '"+searchDTO.getIncome()+"' ");
		if(searchDTO.isWithPhoto())
			queryStr.append(" And url is not null");
		if(searchDTO.isWithHoroscope())
			queryStr.append(" And star is not null and rasi is not null ");
		
		List<Profile> list=dao.search(queryStr.toString());
		List<ProfileDTO> profileDTOList=new ArrayList<ProfileDTO>();
		for(Profile profile:list){
			ProfileDTO profileDTO =new ProfileDTO();
			profileDTO.setAboutself(profile.getAboutself());
			profileDTO.setAddress(profile.getAddress());
			profileDTO.setAge(profile.getAge());
			profileDTO.setBirthplace(profile.getBirthplace());
			profileDTO.setComplexion(profile.getComplexion());
			profileDTO.setDob(profile.getDob());
			profileDTO.setEducation(profile.getEducation());
			profileDTO.setEducationlevel(profile.getEducationlevel());
			profileDTO.setEmail(profile.getEmail());
			profileDTO.setFathername(profile.getFathername());
			profileDTO.setFatherprofession(profile.getFatherprofession());
			profileDTO.setFirstname(profile.getFirstname());
			profileDTO.setGender(profile.getGender());
			profileDTO.setHeight(profile.getHeight());
			profileDTO.setIdProfile(profile.getIdProfile());
			profileDTO.setIncome(profile.getIncome()+"");
			profileDTO.setLastname(profile.getLastname());
			profileDTO.setMaritalstatus(profile.getMaritalstatus());
			profileDTO.setMaternalgotram(profile.getMaternalgotram());
			profileDTO.setMobileno(profile.getMobileno());
			profileDTO.setMothername(profile.getMothername());
			profileDTO.setMotherprofession(profile.getMotherprofession());
			profileDTO.setOccupation(profile.getOccupation());
			profileDTO.setPaternalgotram(profile.getPaternalgotram());
			profileDTO.setRasi(profile.getRasi());
			profileDTO.setStar(profile.getStar());
			profileDTO.setTenantid(profile.getTenantid());
			profileDTO.setVideoUrl("http://www.youtube.com/v/XGSy3_Czz8k");
			profileDTOList.add(profileDTO);
		}
		return profileDTOList;
	}
}
